package com.wei800.turnover.service.vlaibao.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei800.common.util.SnowFlakeUtils;
import com.wei800.common.util.bean.DeepBeanUtils;
import com.wei800.common.util.encrypt.EncryptUtil;
import com.wei800.dto.UserinfoDTO;
import com.wei800.service.vlaibao.IUserinfoService;
import com.wei800.turnover.entity.Userinfo;
import com.wei800.turnover.mapper.vlaibao.IUserinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service // 自动实例化
public class UserinfoServiceImpl
        extends ServiceImpl<IUserinfoMapper, Userinfo> // 为了另外一种更加简单的数据层写法准备的
        implements IUserinfoService<Userinfo> {
    @Autowired
    private IUserinfoMapper userinfoMapper; // 注入对象实例
    @Autowired
    private SnowFlakeUtils snowFlakeUtils; // 雪花ID生成器
    @Override
    public boolean add(UserinfoDTO dto) {
        Userinfo userinfo = new Userinfo(); // 创建Entity实例
        DeepBeanUtils.copyProperties(dto, userinfo); // DTO内容拷贝到Entity对象
        userinfo.setId(snowFlakeUtils.nextId()); // 雪花ID算法
        userinfo.setDeleted(0); // 新增用户不会设置为删除
        userinfo.setCreatetime(LocalDateTime.now()); // 创建日期时间为当前日期时间
        userinfo.setUserpwd(EncryptUtil.encode(userinfo.getUserpwd())); // 密码加密
        return this.userinfoMapper.doCreate(userinfo);
    }

    @Override
    public boolean addBatch(List<UserinfoDTO> dto) {
        List<Userinfo> infos =  DeepBeanUtils.copyListProperties(dto, Userinfo::new); // 对象拷贝
        infos.forEach(user -> {
            user.setId(snowFlakeUtils.nextId()); // 雪花ID算法
            user.setDeleted(0); // 新增用户不会设置为删除
            user.setUserpwd(EncryptUtil.encode(user.getUserpwd())); // 密码加密
            user.setCreatetime(LocalDateTime.now()); // 创建日期时间为当前日期时间
        });
        return this.userinfoMapper.doCreateBatch(infos);
    }

    @Override
    public boolean edit(UserinfoDTO dto) {
        Userinfo userinfo = new Userinfo(); // 创建Entity实例
        DeepBeanUtils.copyProperties(dto, userinfo); // DTO内容拷贝到Entity对象
        return this.userinfoMapper.doUpdate(userinfo);
    }

    @Override
    public boolean remove(Long... ids) {
        Set idSet = Set.of(ids); // 将数据转为Set集合，避免重复
        return this.userinfoMapper.doDelete(idSet); // 数据删除
    }

    @Override
    public Map<String, Object> list(Map<String, Object> params) {
        Integer currentPage = (Integer) params.get("currentPage");
        Integer lineSize = (Integer) params.get("lineSize");
        params.put("startLine", (currentPage - 1) * lineSize);
        if (params.get("keyword") != null) { // 存在关键字
            params.put("keyword", "%" + params.get("keyword") + "%");
        }
        Map<String, Object> result = new HashMap<>();
        result.put("count", this.userinfoMapper.getAllCount(params));
        List<UserinfoDTO> dtoList = DeepBeanUtils.copyListProperties(this.userinfoMapper.findAll(params), UserinfoDTO::new);
        result.put("record", dtoList);
        return result;
    }

    @Override
    public UserinfoDTO get(Long id) {
        Userinfo userinfo = this.userinfoMapper.findById(id);
        UserinfoDTO dto = new UserinfoDTO();
        DeepBeanUtils.copyProperties(userinfo, dto); // DTO内容拷贝到Entity对象
        return dto;
    }

    @Override
    public List<UserinfoDTO> get(Long... ids) {
        List<Userinfo> userinfoList = this.userinfoMapper.findByIds(Set.of(ids));
        List<UserinfoDTO> result =  DeepBeanUtils.copyListProperties(userinfoList, UserinfoDTO::new); // 对象拷贝
        return result;
    }

    @Override
    public UserinfoDTO loginByPhone(UserinfoDTO dto) {
        Userinfo userinfo = this.userinfoMapper.findByPhone(dto.getPhone());
        if (EncryptUtil.encode(dto.getUserpwd()).equals(userinfo.getUserpwd())) { // 密码验证
            UserinfoDTO result = new UserinfoDTO();
            DeepBeanUtils.copyProperties(userinfo, result);
            return result;
        }
        return null;
    }
}
